//
//  WmcPunchCardAlertView.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/5/14.
//

import UIKit
import YPBedrock

// MARK: - [WmcPunchCardAlertView]

class WmcPunchCardAlertView: WmcBaseView {
    // Lifecycle

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    convenience init(currentPunchCardCount: Int) {
        self.init(frame: .zero)
        self.currentPunchCardCount = currentPunchCardCount
        _makeUI()
    }

    @available(*, unavailable)
    public required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // Internal

    /// 当前打卡进度
    var currentPunchCardCount: Int = 1
    /// 打卡天数
    var punchCardCount: Int = 3

    lazy var headImageView: UIImageView = {
        let imageView = UIImageView()
        return imageView
    }()

    lazy var dayStackView: UIStackView = {
        let stackView = UIStackView()
        stackView.alignment = .fill
        stackView.distribution = .equalSpacing
        stackView.spacing = 16
        stackView.axis = .horizontal
        return stackView
    }()

    lazy var promptLabel: UILabel = {
        let label = UILabel.YP.Create(text: "你可在「会员中心-正式积分」查看",
                                      font: 13.wmc_font,
                                      color: UIColor.wmc_grayH8_8A8A99,
                                      alignment: .center)
        return label
    }()

    lazy var lawyerVideoBtn: WmcLawyerVideoBtn = {
        let btn = WmcLawyerVideoBtn.Create()
        return btn
    }()

    lazy var lookBtn: UIButton = {
        let btn = UIButton()
        btn.layer.cornerRadius = 4
        btn.layer.masksToBounds = true
        btn.layer.borderColor = UIColor.wmc_grayH14_CCCCCC.cgColor
        btn.layer.borderWidth = 1
        btn.setTitle("去查看", for: .normal)
        btn.setTitleColor(.wmc_grayH2_323233, for: .normal)
        btn.titleLabel?.font = 16.wmc_font
        return btn
    }()

    lazy var continueBtn: UIButton = {
        let btn = UIButton.Wmc_themeColorBtn(color: .wmc_mainTheme, title: "继续拍照")
        return btn
    }()

    lazy var bottomStackView: UIStackView = {
        let stackView = UIStackView()
        stackView.alignment = .fill
        stackView.distribution = .fillEqually
        stackView.spacing = 16
        stackView.axis = .horizontal
        return stackView
    }()

    func _makeUI() {
        layer.cornerRadius = 8
        layer.shadowColor = UIColor.black.cgColor
        layer.shadowOpacity = 0.2
        layer.shadowRadius = 8
        layer.shadowOffset = .zero

        addSubview(headImageView)
        headImageView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(106.wmc_autoHor)
        }

        addSubview(dayStackView)
        dayStackView.snp.makeConstraints { make in
            make.top.equalTo(headImageView.snp.bottom).offset(24)
            make.left.equalTo(Wmc_standardEdge.left)
            make.right.equalTo(-Wmc_standardEdge.right)
            make.height.equalTo(84)
        }

        var itemsViews: [WmcPunchCardDayView] = []
        var progressPointViews: [UIView] = []
        for i in 0 ..< punchCardCount {
            let isSelect = i < currentPunchCardCount

            let dayView = WmcPunchCardDayView()
            dayView.setSelect(isSelect)
            if i == (punchCardCount - 1) {
                dayView.punchCardDayImageView.image = "business_punch_integral".wmc_image
            }
            dayView.dayLabel.text = "第\(i + 1)天"
            itemsViews.append(dayView)

            let pointView = UIView()
            pointView.backgroundColor = .white
            pointView.layer.borderWidth = 3.2
            pointView.layer.cornerRadius = 8
            if isSelect {
                pointView.layer.borderColor = UIColor.wmc_mainTheme.cgColor
            } else {
                pointView.layer.borderColor = UIColor.wmc_grayH15_E6E6E6.cgColor
            }
            progressPointViews.append(pointView)
        }

        dayStackView.yp.arrangedSubviews = itemsViews

        var beforeDayView: WmcPunchCardDayView?
        progressPointViews.enumerated().forEach { (idx, view) in
            if let dayView = itemsViews.yp.object(at: idx) {
                addSubview(view)
                view.snp.makeConstraints { make in
                    make.top.equalTo(dayView.snp.bottom).offset(10)
                    make.size.equalTo(16)
                    make.centerX.equalTo(dayView)
                }

                if let beforeDayView = beforeDayView {
                    let lineView = UIView()
                    if idx < currentPunchCardCount {
                        lineView.backgroundColor = .wmc_mainTheme
                    } else {
                        lineView.backgroundColor = .wmc_grayH15_E6E6E6
                    }
                    insertSubview(lineView, belowSubview: dayStackView)
                    lineView.snp.makeConstraints { make in
                        make.left.equalTo(beforeDayView.snp.centerX)
                        make.right.equalTo(dayView.snp.centerX)
                        make.centerY.equalTo(view)
                        make.height.equalTo(7.28)
                    }
                }
                beforeDayView = dayView
            }
        }

        var bottomBtns: [UIButton] = []
        if currentPunchCardCount != 3 {
            bottomBtns = [continueBtn]

            headImageView.image = "business_head".wmc_image

            addSubview(lawyerVideoBtn)
            lawyerVideoBtn.snp.makeConstraints { make in
                make.left.right.equalToSuperview()
                make.top.equalTo(dayStackView.snp.bottom).offset(36)
                make.height.equalTo(40)
            }
        } else {
            bottomBtns = [lookBtn, continueBtn]

            headImageView.image = "business_head2".wmc_image

            addSubview(promptLabel)
            promptLabel.snp.makeConstraints { make in
                make.left.right.equalToSuperview()
                make.bottom.equalToSuperview().offset(-70)
                make.height.equalTo(17)
            }
        }

        bottomStackView.yp.arrangedSubviews = bottomBtns

        addSubview(bottomStackView)
        bottomStackView.snp.makeConstraints { make in
            make.bottom.equalTo(-Wmc_standardEdge.bottom)
            make.right.equalTo(-Wmc_standardEdge.right)
            make.left.equalTo(Wmc_standardEdge.left)
            make.height.equalTo(44)
        }
    }
}

// MARK: - WmcAlertViewProtocol

extension WmcPunchCardAlertView: WmcAlertViewProtocol {
    /// 显示
    func show(height: CGFloat) {
        var config = WmcPresentVC.Config(style: .alert(), contentView: self, contentLayoutBlock: { make in
            make.center.equalToSuperview()
            make.left.equalTo(32)
            make.right.equalTo(-32)
            make.height.equalTo(height)
        })
        config.isTouchMaskDismiss = false
        show(config: config)
    }
}
